package com.lh.pospi.x_store.utils;

import android.content.Context;
import android.view.View;

import com.lany.sp.SPHelper;
import com.lh.pospi.x_store.adapter.OrderAdapter;
import com.lh.pospi.x_store.app.MyApp;
import com.lh.pospi.x_store.greendao.BillBeanDao;
import com.lh.pospi.x_store.modle.BillBean;
import com.vondear.rxui.view.dialog.RxDialogSureCancel;

import java.util.ArrayList;
import java.util.List;

public class DaoHelper {
    //查询订单列表
    public static List<BillBean> getBillsNoTime(String type,String stutas){
        MyApp.getInstance().getDaoSession().getBillBeanDao().detachAll();
        List<BillBean> beans = new ArrayList<>();
        switch (stutas) {
            case "全部":
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
            case "已上传":
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)),
                                BillBeanDao.Properties.Status.eq("1"))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
            case "未上传":
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)),
                                BillBeanDao.Properties.Status.eq("0"))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
            default:
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
        }

        return beans;
    }


    //查询订单列表
    public static List<BillBean> getBills(String date ,String type,String stutas){
        MyApp.getInstance().getDaoSession().getBillBeanDao().detachAll();
        List<BillBean> beans = new ArrayList<>();
        switch (stutas) {
            case "全部":
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Billrq.eq(date),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
            case "已上传":
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Billrq.eq(date),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)),
                                BillBeanDao.Properties.Status.eq("1"))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
            case "未上传":
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Billrq.eq(date),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)),
                                BillBeanDao.Properties.Status.eq("0"))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
            default:
                beans = MyApp.getInstance().getDaoSession().getBillBeanDao()
                        .queryBuilder()
                        .where(BillBeanDao.Properties.Type.eq(type),
                                BillBeanDao.Properties.Dbname.eq(SPHelper.getInstance().getString(Constant.DB_NAME)),
                                BillBeanDao.Properties.Billrq.eq(date))
                        .orderDesc(BillBeanDao.Properties.Id)
                        .list();
                break;
        }

        return beans;
    }

    public static void delete(Context mContext,final int position, final List<BillBean> billBeans , final OrderAdapter adapter) {
        final RxDialogSureCancel rxDialogSureCancel = new RxDialogSureCancel(mContext);
        rxDialogSureCancel.setTitle("提示");
        rxDialogSureCancel.setContent("是否删除-" + billBeans.get(position).getBillno() + "?");
        rxDialogSureCancel.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogSureCancel.show();
                MyApp.getInstance().getDaoSession().getBillBeanDao().delete(billBeans.get(position));
                billBeans.remove(position);
                adapter.notifyDataSetChanged();
                rxDialogSureCancel.dismiss();
            }
        });
        rxDialogSureCancel.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rxDialogSureCancel.dismiss();
            }
        });



    }
}
